package com.jobjoin.jobjoin.user.controller;


import com.jobjoin.jobjoin.common.util.R;
import com.jobjoin.jobjoin.user.entity.User;
import com.jobjoin.jobjoin.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author WinsomeC
 * @since 2021-04-18
 */
@RestController
@RequestMapping("/user/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("addUser")
    public R addUser(@RequestBody User user){
        boolean save = userService.save(user);
        if(save){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping
    public R getUser(){
        List<User> list = userService.list(null);
        return R.ok().data("items",list);
    }

    @DeleteMapping("{id}")
    public boolean deleteUserById(@PathVariable String id){
        boolean b = userService.removeById(id);
        return b;
    }

    @GetMapping("getUserById/{id}")
    public R getUserById(@PathVariable String id){
        User user = userService.getById(id);
        return R.ok().data("user",user);
    }

    @PostMapping("updateUser/{id}")
    public R updateUser(@PathVariable String id,@RequestBody User user){
        boolean b = userService.updateById(user);
        if(b){
            return R.ok();
        }else {
            return R.error();
        }

    }


}

